package com.carbon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carbon.dto.WareDto;
import com.carbon.dto.WareNodeTree;
import com.carbon.mapper.CheckMapper;
import com.carbon.service.WareService;
import com.carbon.mapper.WareMapper;
import com.carbon.domain.Ware;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 仓库(Ware)表服务实现类
 *
 * @author WangFK
 * @since 2023-08-21 13:19:47
 */
@Service
public class  WareServiceImpl extends ServiceImpl<WareMapper, Ware> implements WareService {
    @Resource
    private WareMapper wareMapper;
    @Override
    public List<WareNodeTree> getWareTreeNode(List<WareNodeTree> list) {
        List<WareNodeTree> wareNodeTrees = list.stream().filter(item -> {
            if (!"0".equalsIgnoreCase(item.getParendId().toString())) {
                return false;
            }
            return true;
        }).distinct().collect(Collectors.toList());
        // 仓库
        wareNodeTrees.stream().forEach(item->{
            item.setChildren(getWareChildren(item,list));
        });
        return wareNodeTrees;
    }

    @Override
    public WareDto getWareNodeById(Integer wareId) {
        return wareMapper.getWareNodeById(wareId);
    }

    @Override
    public WareDto getWareIdNodeById(Integer wareId) {
        return wareMapper.getWareIdNodeById(wareId);
    }

    public List<WareNodeTree> getWareChildren(WareNodeTree node, List<WareNodeTree> list) {
        List<WareNodeTree> wareList = list.stream().filter(item -> {
            if (!Objects.equals(node.getId(), item.getParendId())) {
                return false;
            }
            return true;
        }).map(item -> {
            item.setChildren(getWareChildren(item, list));
            return item;
        }).collect(Collectors.toList());
        return wareList;

    }
}

